// 微信授权绑定混入
import { userCenterInfo } from "@/api/user.js";
import Routine from "@/libs/routine";
import {
  logInAgain,
  bindingRelationShip,
  entryInvitationCode,
} from "@/api/user.js";

export default {
  data() {
    return {
      userInfoData: {},
      isHave: false, // 是否拥有邀请人
      register: "",
    };
  },
  created() {},
  methods: {
    // 获取用户信息和openId
    getLogInAgainDataes(num, id) {
      // 二次获取用户信息、注重openId
      let uid = null;
      if (num == "1") {
        uid = this.$store.state.app.uid ? this.$store.state.app.uid : id;
      } else {
        uid = id ? id : this.$store.state.app.uid;
      }
      logInAgain(uid).then((res) => {
        if (res.data.key) {
          if (num == "1") {
            this.authKey = res.data.key;
            // this.$refs.loginPopup.open();
            this.$store.commit("SAVEOPENID", res.data.key);
          } else {
            this.$store.commit("SAVEOPENID", res.data.key);
          }
        }
      });
    },
    // // 确定邀请码
    sureBtn() {
      if (this.icode == this.$store.state.app.userInfo.icode) {
        // this.$util.Tips({
        //   title: "不能绑自己的邀请码",
        // });
      } else {
        entryInvitationCode(this.icode)
          .then((res) => {
            // this.$util.Tips({
            //   title: "操作成功",
            // });
          })
          .catch((err) => {
            this.$util.Tips({
              title: err,
            });
          });
      }
    },
    //绑定手机号弹窗回调
    confirmModel() {
      this.$refs.loginPopup.cancel();
    },
    bindPhoneClose(data) {
      let that = this;
      console.log(
        "88888888888888888888",
        that.optionsData,
        this.$store.state.app.openId
      );
      if (data.isStatus) {
        that.$util.Tips(
          {
            title: "授权成功",
            icon: "success",
          },
          {
            tab: 3,
          }
        );

        that.$refs.loginPopup.cancel();
        if (that.userStatus) {
          that.getUserCenterInfoData();
        } else {
          that.getUserCenterInfo();
        }
        // if (Object.keys(that.optionsData).length) {
        //   if (!that.optionsData.tenantId) {
        //     that.getBindingRelationShip("1");
        //   }
        // } else {
        //   that.getBindingRelationShip("1");
        // }
        if (this.icode) that.getBindingRelationShip("1");
      } else {
        that.$refs.loginPopup.cancel();
        if (that.userStatus) {
          that.getUserCenterInfoData();
        } else {
          that.getUserCenterInfo();
        }
        if (this.icode) that.getBindingRelationShip("1");
      }
      setTimeout(() => {
        if (that.optionsData.userId) {
          uni.navigateTo({
            url: `/pages/merchantInfo/inviteBecomeMerchants?userId=${that.optionsData.userId}`,
          });
          // 前往成为推荐官
        } else if (that.optionsData.barCodeStr) {
          uni.navigateTo({
            url: `/pages/supplier/complete?barCodeStr=${that.optionsData.barCodeStr}`,
          });
        }
      }, 1000);
    },
    // 是否绑定关系
    getBindingRelationShip(num) {
      bindingRelationShip().then((res) => {
        this.isHave = res.data;
        console.log("55555555555555555", this.isHave);
        if (this.isHave) {
          if (this.isLogin && num == "1") {
            // this.$refs.improveInformation.open();
          }
        } else {
          this.sureBtn();
        }
        // if (!this.isHave) {
        //   this.$refs.improveInformation.open();
        // }
      });
    },
    getUserProfile() {
      this.$refs.loginPopup.open();
      // let self = this;
      // Routine.getCode()
      //   .then((code) => {
      //     let userInfo = {
      //       code: code,
      //       // spreadPid: this.globalData.spread, //获取推广人ID
      //       type: "routine",
      //     };
      //     self.getWxUser(userInfo);
      //   })
      //   .catch((res) => {});
    },
    // 登录调用方法
    getWxUserData(data) {
      let self = this;
      this.register = data.type || "";
      self.authKey = data.key;
      if (!data.key) {
        self.getLogInAgainDataes("1", data.id);
      } else {
        this.$store.commit("SAVEOPENID", data.key);
        this.operationLogin = true;
      }
    },
    /**
     *
     * 获取个人中心详情
     */
    getUserCenterInfo: function () {
      userCenterInfo()
        .then((res) => {
          let data = res.data;
          this.userInfoData = res.data;
          this.$store.commit("UPDATE_USERINFO", {
            avatar: data.avatar,
            nikeName: data.nickname,
            phone: data.phone,
            voucher: data.voucher,
            code: data.code,
            icode: data.icode,
            identityName: data.identityName,
            identity: data.identity,
          });
          this.$store.commit("SETUID", data.id);
          // 首页如果登录过保存用户信息
          if (this.optionsData.icode || this.optionsData.scene) {
            this.icode = this.optionsData.icode;
            if (this.optionsData.scene) {
              let list = this.optionsData.scene.split("3D");
              this.icode = list[2];
              // this.icode = this.optionsData.scene.slice(-4) + "TY";
            }
            // this.$refs.improveInformation.open();
            // this.getBindingRelationShip();
          }
        })
        .catch((err) => {});
    },
  },
};
